<template>
	<view class="container">
		<TopNav title="设置"></TopNav>
		<view class="radius-6 bg-white p-l16 p-r10">
			<view class="flex justify-between align-center p-tb14">
				<text class="f-s15 f-w400 text-dark line-height20">是否免费出行</text>
				<view class="flex align-center">
					<u-switch v-model="switchVal" size="20" activeColor="#FE3F59" inactiveColor="#B2B8C1"
						@change="handleSwitchChange"></u-switch>
				</view>
			</view>
		</view>
		<view class="radius-6 bg-white p-l16 p-r10 p-tb16 m-t10" v-for="(item,index) in cellList" :key="index">
			<view class="flex justify-between align-center" :class="{'m-b20':subIndex!==item.length-1}"
				v-for="(subItem,subIndex) in item" :key="subIndex" @click="handleToPage(subItem.path)">
				<text class="f-s15 f-w500 text-dark line-height20">{{subItem.text}}</text>
				<view class="flex align-center">
					<text class="line-height20 f-w400 f-s15 m-r4 text-gray"
						v-if="subItem.path === 'version'">{{version}}</text>
					<text class="line-height20 f-w400 f-s15 m-r4 text-gray" v-else>{{subItem.value}}</text>
					<image src="/static/images/mine/dada-more.png" class="width-20 height-20" mode=""></image>
				</view>
			</view>
		</view>
		<view class="self-btn m-t16 margin-center" hover-class="pressed" @click="handleLogout">
			<text>退出登录</text>
		</view>
		<Toast></Toast>
		<Modal :show.sync="modalShow" content="确认退出登录" @confirm="handleLogoutConfirm"></Modal>
	</view>
</template>

<script>
	import Modal from "@/components/modal"
	import {
		getInfo
	} from '@/api/login';
	import {
		setWorkStatus
	} from "@/api/index/set.js"
	import {
		appConfig
	} from "@/api/document"
	import APPUpdate from '@/uni_modules/zhouWei-APPUpdate/js_sdk/appUpdate'
	export default {
		components: {
			Modal
		},
		data() {
			return {
				modalShow: false,
				switchVal: false,
				version: '1.0.0',
				cellList: [
					[{
						text: '营业执照',
						path: 'business-license'
					}, {
						text: '资质认证',
						path: 'certification'
					}, {
						text: '健康证',
						path: 'health'
					}],
					[{
						text: '更新简介',
						path: 'resume'
					}, {
						text: '无犯罪证明',
						path: 'no-crime'
					}, {
						text: '更新身份证',
						path: 'identity-card'
					}, {
						text: '紧急联系人',
						path: 'contact-person'
					}],
					[{
						text: '隐私政策',
						path: '/pages/common/document/index?title=隐私政策'
					}, {
						text: '注销账号',
						path: 'sign-out'
					}, {
						text: '当前版本',
						path: 'version',
						value: ''
					}]
				]
			};
		},
		onLoad() {
			this.getDetail()
			// #ifdef APP
			plus.runtime.getProperty(plus.runtime.appid, (info)=> {
				this.version = info.version
			});
			this.getAppVersion()
			// #endif
		},
		methods: {
			getAppVersion() {
				// 获取应用配置
				appConfig().then(res => {
					let {
						data: {
							app_version
						}
					} = res
				}).catch(() => {})
			},
			handleSwitchChange(val) {
				// 设置免费出行状态
				this.$modal.loading('设置中').then(() => {
					setWorkStatus({
						field: 'is_free',
						value: val ? 1 : 0
					}).then(res => {
						this.$modal.closeLoading()
						this.$toast("设置成功")
					}).catch(() => this.$modal.closeLoading())
				})
			},
			getDetail() {
				getInfo().then(res => {
					let {
						data: {
							info
						}
					} = res
					this.switchVal = Boolean(info?.is_free)
				})
			},
			handleLogoutConfirm() {
				this.$store.dispatch('LogOut').then(() => {
					this.modalShow = false
					this.$tab.reLaunch('/pages/login/index')
				})
			},
			handleLogout() {
				this.modalShow = true
			},
			handleToPage(path) {
				if (path === 'version') {
					APPUpdate(true)
					return
				}
				this.$tab.navigateTo(path)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx 24rpx;
	}
</style>